package com.zeropol2.anyquestion.component.ui.list;

import java.util.List;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.zeropol2.anyquestion.R;
import com.zeropol2.anyquestion.component.ui.listitem.ArticleView;

/**
 * @author zeropol2
 * 글목록 리스트
 */
/**
 * @author zeropol2
 *
 */
/**
 * @author zeropol2
 *
 */
public class ArticleViewList extends LinearLayout {

	private Context mContext;
	private LayoutInflater mInflater;
	private LinearLayout mLayout;
	private ListView mListView;
	private List<ArticleView> mList;
	private OnItemSelectedListener mItemSelectedListener;

	public ArticleViewList(Context context) {
		super(context);
		mContext = context;
		init();
	}

	public ArticleViewList(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		init();
	}

	private void init() {
		mInflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mLayout = (LinearLayout) mInflater.inflate(R.layout.article_view_list,
				this);
		mListView = (ListView) mLayout
				.findViewById(R.id.article_view_list_list);
	}

	/**
	 * 리스트에 아이템 꽂아주는 메소드
	 * @param list 
	 */
	public void setListItem(List<ArticleView> list) {
		mList = list;
		mListView.setAdapter(new ArticleViewListAdapter(mContext,
				R.layout.article_view, mList));
	}
	
	/**
	 * 리스너 설정 메소드
	 * @param l
	 */
	public void setItemSelectedListener(OnItemSelectedListener l) {
		mItemSelectedListener = l;
		mListView.setOnItemSelectedListener(mItemSelectedListener);
	}

	/**
	 * @author zeropol2
	 * 글목록 리스너 어댑터
	 */
	private class ArticleViewListAdapter extends ArrayAdapter {
		private int mViewId;

		public ArticleViewListAdapter(Context context, int viewId,
				List<ArticleView> list) {
			super(context, viewId, list);
			mViewId = viewId;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder = new ViewHolder();
			if (null == convertView) {
				convertView = mInflater.inflate(mViewId, null);
				viewHolder.title = (TextView) convertView
						.findViewById(R.id.article_view_title);
				viewHolder.date = (TextView) convertView
						.findViewById(R.id.article_view_date);
				viewHolder.replyCount = (TextView) convertView
						.findViewById(R.id.article_view_reply_count);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}
			viewHolder.title.setText(mList.get(position).getTitle());
			viewHolder.date.setText(mList.get(position).getDate());
			viewHolder.replyCount.setText(mList.get(position).getReplyCount());
			return convertView;

		}
	}

	/**
	 * @author zeropol2
	 * 리스트 성능향상을 위한 뷰홀더
	 */
	private class ViewHolder {
		public TextView title;
		public TextView date;
		public TextView replyCount;
	}

}
